package com.gangquan360.smartadmin.module.customer.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.customer.dao.CustomerTagDao;
import com.gangquan360.smartadmin.module.customer.domain.dto.CustomerTagDTO;
import com.gangquan360.smartadmin.module.customer.domain.dto.CustomerTagQueryDTO;
import com.gangquan360.smartadmin.module.customer.domain.entity.CustomerTagEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 客户标签表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-07 17:07:31
 * @since JDK1.8
 */
@Service
public class CustomerTagService {

    @Autowired
    private CustomerTagDao customerTagDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-07 17:07:31
     */
    public ResponseDTO<PageResultDTO<CustomerTagDTO>> queryByPage(CustomerTagQueryDTO queryDTO) {
        Page<CustomerTagDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<CustomerTagEntity> entities = customerTagDao.queryByPage(page, queryDTO);
        List<CustomerTagDTO> dtoList = SmartBeanUtil.copyList(entities, CustomerTagDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<CustomerTagDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }
    
    
    /**
     * 
     * @return
     */
    public ResponseDTO<List<CustomerTagDTO>> queryList() {
		CustomerTagEntity entity = new CustomerTagEntity();
		Wrapper<CustomerTagEntity> wrapper = new EntityWrapper<CustomerTagEntity>(entity);
		List<CustomerTagEntity> selectList = customerTagDao.selectList(wrapper);
		List<CustomerTagDTO> copyList = SmartBeanUtil.copyList(selectList, CustomerTagDTO.class);
    	return ResponseDTO.succData(copyList);
    }
    

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-07 17:07:31
     */
    public ResponseDTO<String> add(CustomerTagDTO addDTO) {
        CustomerTagEntity entity = SmartBeanUtil.copy(addDTO, CustomerTagEntity.class);
        customerTagDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-07 17:07:31
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(CustomerTagDTO updateDTO) {
        CustomerTagEntity entity = SmartBeanUtil.copy(updateDTO, CustomerTagEntity.class);
        customerTagDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-07 17:07:31
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        customerTagDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-07 17:07:31
    */
    public ResponseDTO<CustomerTagDTO> detail(Long id){
        CustomerTagEntity entity = customerTagDao.selectById(id);
        CustomerTagDTO dto =  SmartBeanUtil.copy(entity,CustomerTagDTO.class);
        return ResponseDTO.succData(dto);
    }
}
